package com.common.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.common.user.domain.Organization;
import com.common.user.repository.OrganizationRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * Organization api
 * @author enbool
 *
 */
@Api(description = "Manage Organization")
@RestController
@RequestMapping(value = "/organizations")
public class OrganizationController {
	
	@Autowired
	OrganizationRepository organizationRepository;
	
	@ApiOperation(value = "Create a Organization")
	@PostMapping
	public Organization create(@RequestBody Organization Organization) {
		
		return organizationRepository.save(Organization); 
	}
	
	@ApiOperation(value = "Delete a organization")
	@ApiImplicitParam(name = "id", value = "The id of the Organization to delete.")
	@DeleteMapping(value = "/{id:\\d+}")
	public void delete(@PathVariable Long id) {
		
		organizationRepository.delete(id);
	}
	
	@ApiOperation(value = "Update a Organization")
	@PutMapping
	public Organization update(@RequestBody Organization Organization) {
		
		return organizationRepository.save(Organization);
	}
	
	@ApiOperation(value = "List of Organizations")
	@GetMapping
	public List<Organization> findAll() {
		
		return organizationRepository.findAll();
	}
	
	@ApiOperation(value = "Get a user")
	@ApiImplicitParam(name = "id", value = "The id of the organization to get.")
	@GetMapping(value = "/{id:\\d+}")
	public Organization findOne(@PathVariable Long id) {
		
		return organizationRepository.getOne(id);
	}

}
